$OpenBSD: patch-bytearray_stubs_c,v 1.1 2017/03/13 22:25:14 giovanni Exp $

Fix rare SIGSEGV when transferring large replicas.
Fix a theoretical integer overflow. 

References:
https://github.com/bcpierce00/unison/commit/c1ddff13aa96b124680cce61673129aeb563dbf7
https://github.com/bcpierce00/unison/commit/f59663d67f4593a5bc1e554058fe6864751e805e

Thanks to Alex Markley and OCaml developers
--- bytearray_stubs.c.orig	Mon May 23 18:40:05 2016
+++ bytearray_stubs.c	Sun Mar 12 20:41:53 2017
@@ -5,6 +5,7 @@
 
 #include "caml/intext.h"
 #include "caml/bigarray.h"
+#include "caml/memory.h"
 
 CAMLprim value ml_marshal_to_bigarray(value v, value flags)
 {
@@ -21,15 +22,18 @@ CAMLprim value ml_marshal_to_bigarray(value v, value f
 
 CAMLprim value ml_unmarshal_from_bigarray(value b, value ofs)
 {
+  CAMLparam1(b); /* Holds [b] live until unmarshalling completes. */
+  value result;
   struct caml_bigarray *b_arr = Bigarray_val(b);
-  return input_value_from_block (Array_data (b_arr, ofs),
+  result = input_value_from_block (Array_data (b_arr, ofs),
                                  b_arr->dim[0] - Long_val(ofs));
+  CAMLreturn(result);
 }
 
 CAMLprim value ml_blit_string_to_bigarray
 (value s, value i, value a, value j, value l)
 {
-  char *src = String_val(s) + Int_val(i);
+  char *src = String_val(s) + Long_val(i);
   char *dest = Array_data(Bigarray_val(a), j);
   memcpy(dest, src, Long_val(l));
   return Val_unit;
